///:SQLParameter.java
package cn.weyoungstudio.hwes.jdbc.common;

import java.io.Serializable;
import java.sql.Date;
import java.sql.Types;

/**
 * @author icechen1219
 * @date 2019/01/03
 */
public class SQLParameter implements Serializable {
    /**
     * 自动判断类型
     */
    public static final int AUTO_TYPE = -9999;

    /**
     * 输入参数值的对象，如果是基本类型，则需要转换成相应的对象，如int型需转换成Integer
     */
    private Object value;
    /**
     * 数据库的 SQL 类型（定义于 java.sql.Types 中）
     */
    private int targetSqlType;

    private SQLParameter() {

    }

    public SQLParameter(Object value) {
        this(value, AUTO_TYPE);
    }

    public SQLParameter(Object value, int targetSqlType) {
        this.value = value;
        this.targetSqlType = targetSqlType;
    }


    /**
     * javaType映射JdbcType
     *
     * @param javaType
     * @return 对应的数据库类型
     */
    public static int convert2JdbcType(Class<?> javaType) {
        if (int.class.equals(javaType) || Integer.class.equals(javaType)) {
            return Types.INTEGER;
        }
        if (String.class.equals(javaType)) {
            return Types.VARCHAR;
        }
        if (Long.class.equals(javaType) || long.class.equals(javaType)) {
            return Types.BIGINT;
        }
        if (char.class.equals(javaType) || Character.class.equals(javaType)) {
            return Types.CHAR;
        }
        if (Float.class.equals(javaType) || float.class.equals(javaType)) {
            return Types.FLOAT;
        }
        if (Double.class.equals(javaType) || double.class.equals(javaType)) {
            return Types.DOUBLE;
        }
        if (Date.class.equals(javaType)) {
            return Types.TIMESTAMP;
        }
        if (byte[].class.equals(javaType)) {
            return Types.BLOB;
        }
        if (Boolean.class.equals(javaType) || boolean.class.equals(javaType)) {
            return Types.DOUBLE;
        }

        return Types.OTHER;
    }

    public int getTargetSqlType() {
        return targetSqlType;
    }

    public void setTargetSqlType(int targetSqlType) {
        this.targetSqlType = targetSqlType;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "SQLParameter{" +
                ", value=" + value +
                ", targetSqlType=" + targetSqlType +
                '}';
    }
}
///:SQLParameter.java
